<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件
//权限验证
if(!function_exists('authcheck')){
    function authcheck($name, $uid,$auth='', $type=1, $mode='url', $relation='or'){
        if(in_array($uid,config('app.ADMINISTRATOR'))){
            return true;
        }else if(in_array($name, config('app.NO_AUTH_RULES'))){
            return true;
        }else{
            if(!$auth){
                $auth=new \luck\Auth();
            }

            return $auth->check($name, $uid, $type, $mode, $relation)?true:false;
        }
    }
}
/**
 * 当天末12点
 * @param
 * @acthor:式神 289650682@qq.com
 * @return 2019-01-31
 */
if(!function_exists('end_day')) {
    function end_day($time)
    {
        $d = date('d',$time);
        $y = date('Y',$time);
        $m = date('m',$time);
        return strtotime($y.'-'.$m.'-'.$d.' 23:59:59');
    }
}
if(!function_exists('next_day')) {
    function next_day($time)
    {
        $day = date('d',$time);
        $year = date('Y',$time);
        $m = date('m',$time);
        $nextY = $year;
        $nextM = $m;
        switch ($m){
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
                if($day==31){
                    $nextDay=1;
                    $nextM=$m+1;
                }else{
                    $nextDay=$day+1;
                }
                break;
            case 12:
                if($day==31){
                    $nextDay=1;
                    $nextM=1;
                    $nextY=$year+1;
                }else{
                    $nextDay=$day+1;
                }
                break;
            case 4:
            case 6:
            case 9:
            case 11:
                if($day==30){
                    $nextDay=1;
                    $nextM=$m+1;
                }else{
                    $nextDay=$day+1;
                }
                break;
            case 2:
                if($year%4 == 0){
                    if($day==29){
                        $nextDay=1;
                        $nextM=$m+1;
                    }else{
                        $nextDay=$day+1;
                    }
                }else{
                    if($day==28){
                        $nextDay=1;
                        $nextM=$m+1;
                    }else{
                        $nextDay=$day+1;
                    }
                }
                break;
        }
        return strtotime($nextY.'-'.$nextM.'-'.$nextDay.' 23:59:59');
    }
}

/**
 * 权限树
 */
if(!function_exists('rule_tree')) {
    function rule_tree($cate, $disabled = [], $pidname = 'pid', $pid = 0)
    {
        $arr = array();
        foreach ($cate as $k => $v) {
            if(!isset($v['title'])){
                $v["title"]=$v["name"];
            }
            if ($v[$pidname] == $pid) {
                $v['text'] = isset($v['title'])? $v['title']:$v['name'];//下拉tree菜单名字
                if (count($disabled) > 0) {
                    if (in_array($v['id'], $disabled)) {
                        $v['disabled'] = true;
                    }
                }
                $v['key'] = $v['value'] = $v['id'];
                if(count(rule_tree($cate, $disabled, $pidname, $v['id']))>0){
                    $v['children'] = rule_tree($cate, $disabled, $pidname, $v['id']);
                }
                $arr[] = $v;
            }
        }
        return $arr;
    }
}

/**
 * 多级分类树形
 * @param
 * @acthor:式神 289650682@qq.com
 * @return 2016-10-12
 */
if(!function_exists('data_tree')) {
    function data_tree($cate, $pidname = 'pid', $pid = 0, $rule = [])
    {
        $arr = array();
        foreach ($cate as $k => $v) {
            if(!isset($v['title'])){
                $v["title"]=$v["name"];
            }
            if ($v[$pidname] == $pid) {
                $v['text'] = isset($v['title'])? $v['title']:$v['name'];//下拉tree菜单名字
                if (count($rule) > 0) {
                    if (in_array($v['id'], $rule)) {
                        $v['disabled'] = true;
                    }
                }
                $v['key'] = $v['value'] = $v['id'];
                /*if ($open != 'open' && count(getChildsId($cate, $v['id'])) == 0) {
                    $v['state'] = 'open';
                } else {
                    $v['state'] = $open;//关闭菜单
                }*/
                if(count(data_tree($cate, $pidname, $v['id'], $rule))>0){
                    $v['children'] = data_tree($cate, $pidname, $v['id'], $rule);
                }
                $arr[] = $v;
            }
        }
        return $arr;
    }
}
/**
 * 禁止上级选择
 * @param
 * @acthor:式神 289650682@qq.com
 * @return 2019-03-05
 */
if(!function_exists('cate_tree')) {
    function cate_tree($cate, $pidname = 'pid', $pid = 0, $rule = [])
    {
        $arr = array();
        foreach ($cate as $k => $v) {
            if(!isset($v['title'])){
                $v["title"]=$v["name"];
            }
            if ($v[$pidname] == $pid) {
                $v['text'] = isset($v['title'])? $v['title']:$v['name'];//下拉tree菜单名字
                if (count($rule) > 0) {
                    if (in_array($v['id'], $rule)) {
                        $v['disabled'] = true;
                    }
                }
                $v['key'] = $v['value'] = $v['id'];
                if(count(cate_tree($cate, $pidname, $v['id'], $rule))>0){
                    $v['disabled'] = true;
                    $v['children'] = cate_tree($cate, $pidname, $v['id'], $rule);
                }
                $arr[] = $v;
            }
        }
        return $arr;
    }
}
/**
 * 查询子栏目id
 * @acthor:式神 2896506822qq.com
 * @time: 2016-10-12
 */
//子栏目ID
if(!function_exists('getChildsId')) {
    function getChildsId($cate, $pid = 0, $pidname = 'pid')
    {
        $arr = array();
        foreach ($cate as $v) {
            if ($v[$pidname] == $pid) {
                $arr[] = $v['id'];
                $arr = array_merge($arr, getChildsId($cate, $v['id'], $pidname));
            }
        }
        return $arr;
    }
}
//父栏目ID
if(!function_exists('getCateId')) {
    function getCateId($cate, $id)
    {
        $arr = array();
        foreach ($cate as $v) {
            if ($v['id'] == $id) {
                $arr[] = $v['pid'];
                $arr = array_merge(getCateId($cate, $v['pid']), $arr);
            }
        }
        return $arr;
    }
}

//清理缓存函数
if (!function_exists('delete_dir_file'))
{
    /**
     * 循环删除目录和文件
     * @param string $dir_name
     * @return bool
     */
    function delete_dir_file($dir_name) {
        $result = false;
        if(is_dir($dir_name)){
            if ($handle = opendir($dir_name)) {
                while (false !== ($item = readdir($handle))) {
                    if ($item != '.' && $item != '..') {
                        if (is_dir($dir_name . DIRECTORY_SEPARATOR . $item)) {
                            delete_dir_file($dir_name . DIRECTORY_SEPARATOR . $item);
                        } else {
                            unlink($dir_name . DIRECTORY_SEPARATOR . $item);
                        }
                    }
                }
                closedir($handle);
                if (rmdir($dir_name)) {
                    $result = true;
                }
            }
        }
        return $result;
    }
}